//
//  CityVC.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/6/2.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

typealias StringCallback = (String) -> Void

class CityVC: UIViewController {

    let presenter = CityPresenter.init()
    
    var onComplete:StringCallback?
    var singleSelect = false
    var selected : [String] = []
    
    init() {
       super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "选择地区"
        
        self.view.addSubview(presenter.view)
        
        presenter.view.contentInset = UIEdgeInsets.init(top: 64, left: 0, bottom: 48, right: 0)
        presenter.singleSelect = singleSelect
        presenter.selected = self.selected
        
        let bottom = UIView.init(frame: CGRect.init(x: 0, y: Const.SCREEN_HEIGHT - 68, width: Const.SCREEN_WIDTH, height: 68))
        bottom.backgroundColor = Const.WTM_WHITE_SMOKE
        self.view.addSubview(bottom)
        let button = UIButton.init(type: .roundedRect)
        button.frame = CGRect.init(x: 10, y: 10, width: Const.SCREEN_WIDTH - 20, height: 44)
        button.backgroundColor = Const.WTM_GREEN
        button.addTarget(self, action: #selector(KXChannelVC.onTapConfirm), for: .touchUpInside)
        button.setTitle("确定", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.layer.cornerRadius = 4
        bottom.addSubview(button)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func onTapConfirm() {
        
        let result = self.presenter.selected
        let result2 = result.reduce("") { (result, city) -> String in
            result + city
        }
        if let function = self.onComplete  {
            function(result2)
        }
        self.navigationController?.popViewController(animated: true)
    }
}
